﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AllegroIWI
{
	public class SimiliarHistogramChiComparer : Comparer<SearchResult>
	{
		public override int Compare(SearchResult x, SearchResult y)
		{
			int diff = 0;
			if (x != null && x.Image != null && x.Image.Histogram != null &&
				y != null && x.Image != null && x.Image.Histogram != null)
			{
				for (int i = 0; i < 256; i++)
				{
					if (x.Image.Histogram.RGB[i]!=0)
						diff += (x.Image.Histogram.RGB[i] - y.Image.Histogram.RGB[i])^2 / x.Image.Histogram.RGB[i];
				}
				return diff;
			}
			else
				return diff;
		}
	}
}
